package com.aconcagua.samples.charts.state.model {
  import flash.events.Event;
  import flash.events.EventDispatcher;
  
  import mx.binding.utils.BindingUtils;
  import mx.charts.chartClasses.Series;
  import mx.charts.series.AreaSeries;
  import mx.collections.ArrayCollection;
  
  public class Series extends EventDispatcher {
    private static var _idCounter: uint = 0;
    
    private var _id: Number;
    private var _data: ArrayCollection;
    private var _ownerChart: Chart;

    public function Series(seriesData: ArrayCollection) { 
      _id = _idCounter++;
      _data = seriesData;
//      _ownerChart = owner;
    }

    model function set data(seriesData: ArrayCollection): void {
      _data = seriesData;
      this.dispatchEvent(new Event("data_changed"));
    }

/* 
    model function zoom(dateFrom: Date, dateTo: Date): void {
      for each(var dataElement: SeriesElement in _data
    }
 */

    [Bindable(event="data_changed")]
    public function get length(): Number {
      return _data.length;
    }

    /**
    * @returns an ArrayCollection of data points
    **/
    [Bindable(event="data_changed")]
    public function get data(): ArrayCollection {
      return _data;
    }

    [Bindable(event="data_changed")]
    public function getElementAt(idx: int): SeriesElement {
      return _data.getItemAt(idx) as SeriesElement;
    }

    public function asFlexSeries(): mx.charts.chartClasses.Series {
      var series: AreaSeries = new AreaSeries();
      BindingUtils.bindProperty(series, "dataProvider", this, "data");
      series.xField = "date";
      series.yField = "value";

      return series;
    }
  }
}